# -*- coding: utf-8 -*-
########################################################################
#   Pax-Selector. Copyright 2010, Hector Sanjuán.                      #
#                                                                      #
#   Pax-selector is free software: you can redistribute it and/or      #
#   modify it under the terms of the GNU General Public License as     #
#   published by the Free Software Foundation, either version 3 of     #
#   the License, or any later version.                                 #
#                                                                      #
#   Pax-selector is distributed in the hope that it will be useful,    #
#   but WITHOUT ANY WARRANTY; without even the implied warranty of     #
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the      #
#   GNU General Public License for more details.                       #
#                                                                      #
#   You should have received a copy of the GNU General Public License. #
#   If not, see <http://www.gnu.org/licenses/>.                        #
#                                                                      #
########################################################################

require 'csv'
require 'paxUtils'

PAX_LIST="pax_list.csv"


class PaxList
  attr_reader :fields, :rankfields, :customfields
  
  def initialize
    @fields = PaxUtils.readFields
    @rankfields = PaxUtils.readFields 'RANKFIELDS'
    @customfields = PaxUtils.readFields 'CUSTOMFIELDS'
    @group = []
    first = true
    id = 1
    CSV.foreach(PAX_LIST, :col_sep => ";" ) do |row|
      if first then first=false; next; end
      indiv = Hash[*@fields.zip(row).flatten]
      indiv[:id] = id
      @group << indiv
      id += 1
    end
  end
   
  def indiv id
    @group[id-1]
  end

  def print
    @group.each do |participant|
      participant.each { |k,v| puts "#{k} => #{v}" }
    end
  end

  def each
    @group.each do | elem |
      yield elem
    end
  end
  
  def size
    @group.size
  end
      
end
